package com.linx.common.web.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.linx.common.core.utils.SpringUtils;
import com.linx.common.web.domain.bo.*;
import com.linx.common.web.domain.vo.*;
import com.linx.common.web.properties.HkProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @ClassName FaceSingleControlUtils
 * @Description 人脸操作工具类
 * @Author gushaoliang
 * @Date 2024/12/4 下午6:34
 * @Version 1.0
 */
@Component
@Slf4j
public class FaceSingleControlUtils {
    private String agreement;
    private String host;
    private String appKey;
    private String appSecret;
    private String artemisPath;


    public FaceSingleControlUtils() {
        HkProperties hkProperties = SpringUtils.getBean(HkProperties.class);
        agreement = hkProperties.getAgreement();
        host = hkProperties.getHost();
        appKey = hkProperties.getAppKey();
        appSecret = hkProperties.getAppSecret();
        artemisPath = hkProperties.getArtemisPath();
    }

    //添加人脸
    public  ResponseVo add(FaceSingleAddRequest addRequest) throws Exception {
        String apiPath = artemisPath +"/api/resource/v1/face/single/add";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", artemisPath + apiPath);
        Map<String,String> headers = new HashMap<>();
        headers.put("tagId", addRequest.getTagId());
        String body=JSON.toJSONString(addRequest);
        String json = ArtemisHttpUtil.doPostStringArtemis( path, body, null,  null, "application/json", headers);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);

        ResponseVo<FaceSingleAddVo> response = JSON.parseObject(
            json,
            new TypeReference<ResponseVo<FaceSingleAddVo>>() {}
        );
        return response;
    }

    //修改人脸
    public  ResponseVo update(FaceSingleUpdateRequest updateRequest) throws Exception {
        String apiPath = artemisPath +"/api/resource/v1/face/single/update";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", artemisPath + apiPath);

        Map<String,String> headers = new HashMap<>();
        headers.put("tagId", updateRequest.getTagId());
        String body=JSON.toJSONString(updateRequest);
        String json = ArtemisHttpUtil.doPostStringArtemis( path, body, null,  null, "application/json", headers);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);

        ResponseVo<FaceSingleUpdateVo> response = JSON.parseObject(
            json,
            new TypeReference<ResponseVo<FaceSingleUpdateVo>>() {}
        );
        return response;
    }


}
